package com.chengh.nami.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.chengh.nami.common.constants.UserConstants;
import com.chengh.nami.common.domain.pojo.SysMenu;
import com.chengh.nami.common.exception.BizException;
import com.chengh.nami.common.utils.StringUtils;
import com.chengh.nami.system.domain.vo.MetaVo;
import com.chengh.nami.system.domain.vo.RouterVo;
import com.chengh.nami.system.mapper.SysMenuMapper;
import com.chengh.nami.system.service.ISysMenuService;
import lombok.AllArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * @author chengh
 */
@Service
@Slf4j
@AllArgsConstructor
public class SysMenuServiceImpl extends ServiceImpl<SysMenuMapper, SysMenu> implements ISysMenuService {

    @Override
    public List<SysMenu> getMenuByUserId(Long userId) {
        return this.baseMapper.getMenuByUserId(userId);
    }

    @Override
    public List<SysMenu> getMenuByRoleId(Long roleId) {
        return this.baseMapper.getMenuByRoleId(roleId);
    }

    @Override
    public List<SysMenu> getList(SysMenu sysMenu) {
        return this.baseMapper.selectList(new LambdaQueryWrapper<SysMenu>()
                .like(StringUtils.isNotEmpty(sysMenu.getMenuName()), SysMenu::getMenuName, sysMenu.getMenuName())
                .eq(null != sysMenu.getStatus(), SysMenu::getStatus, sysMenu.getStatus())
                .orderByAsc(SysMenu::getOrderNum)
        );
    }

    @Override
    public void addMenu(SysMenu sysMenu) {
        if (UserConstants.TYPE_DIR.equals(sysMenu.getMenuType())) {
            if (sysMenu.getParentId() == 0L) {
                sysMenu.setComponent(UserConstants.LAYOUT);
            } else {
                sysMenu.setComponent(UserConstants.PARENT_VIEW);
            }
        }
        // 根据名称查询菜单信息
        List<SysMenu> sysMenus = this.baseMapper.selectList(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getMenuName, sysMenu.getMenuName()));
        if (!CollectionUtils.isEmpty(sysMenus)) {
            throw new BizException("菜单名称重复");
        }
        this.baseMapper.insert(sysMenu);
    }

    @Override
    public boolean deleteById(Long id) {
        List<SysMenu> menus = this.list(new LambdaQueryWrapper<SysMenu>().eq(SysMenu::getParentId, id));
        if (!CollectionUtils.isEmpty(menus)) {
            throw new BizException("该菜单存在子菜单，请先删除子菜单。");
        }
        return this.removeById(id);
    }

    @Override
    public SysMenu getMenuById(Long id) {
        return this.getById(id);
    }

    @Override
    public List<RouterVo> buildMenus(List<SysMenu> menus) {
        List<RouterVo> routers = new LinkedList<>();
        for (SysMenu menu : menus) {
            RouterVo router = new RouterVo();
            router.setName(menu.getMenuName());
            router.setPath(menu.getPath());
            router.setHidden("1".equals(menu.getVisible()));
            router.setComponent(getComponent(menu));
            router.setQuery(menu.getQuery());

            List<SysMenu> cMenus = menu.getChildren();
            // 目录  填充属性
            if (UserConstants.TYPE_DIR.equals(menu.getMenuType())) {
                router.setAlwaysShow(true);
                router.setRedirect("noRedirect");
                router.setChildren(buildMenus(cMenus));
            }

            // mate
            MetaVo metaVo = new MetaVo();
            metaVo.setIcon(menu.getIcon());
            metaVo.setTitle(menu.getMenuName());
            metaVo.setNoCache(StringUtils.equals("1", menu.getIsCache()));
            metaVo.setLink(getLink(menu));
            router.setMeta(metaVo);
            routers.add(router);
        }
        return routers;
    }

    /**
     * 设置link
     */
    public static String getLink(SysMenu menu) {
        return UserConstants.YES_FRAME.equals(menu.getIsFrame()) ? menu.getPath() : null;
    }

    /**
     * 获取组件信息
     *
     * @param menu 菜单信息
     * @return 组件信息
     */
    private String getComponent(SysMenu menu) {
        return menu.getComponent();
    }


}
